<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class ad_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    public function index()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '15';
        $status = isset($_GET['status']) ? $_GET['status'] : '';
        $wsql = '';
        if ($status) {
            $wsql .= ' start_time < ' . time() . ' and end_time > ' . time() . " and ";
        } elseif ($status == '0') {
            $wsql .= ' start_time > ' . time() . ' or end_time < ' . time() . " and ";
        }
        $pid = !empty($_GET['pid']) ? $_GET['pid'] : '';
        if ($pid) {
            $wsql .= ' postion = ' . $wsql;
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $ad_type = $this->ad_type();
        $query = $this->db->get_all("select * from " . table('ad') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($query) {
            foreach ($query as $v) {
                $cid_arr[] = $v['postion'];
            }
            if (isset($cid_arr)) {
                $cid_in = implode(',', $cid_arr);
                $cquery = $this->db->get_all("select * from " . table('ad_postion') . " where id in(" . $cid_in . ")");
                foreach ($cquery as $v) {
                    $pst_arr[$v['id']] = $v['name'];
                }
            }
            foreach ($query as $v) {
                $v['start_time'] = _date($v['start_time']);
                $v['end_time'] = _date($v['end_time']);
                $v['addtime'] = _date($v['addtime']);
                $v['type_name'] = $ad_type[$v['type']];
                if (isset($pst_arr[$v['postion']])) {
                    $v['postion_name'] = $pst_arr[$v['postion']];
                }
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(*) as total from " . table('ad') . $wsql);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function edit()
    {
        $id = isset($_GET['id']) ? intval($_GET['id']) : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('ad') . " where id = " . $id);
            $row['start_time'] = date('Y-m-d', $row['start_time']);
            $row['end_time'] = date('Y-m-d', $row['end_time']);
            $this->assign('row', $row);
        }
        $this->assign('postion', $this->get_postion());
        $this->display();
    }
    public function save()
    {
        if ($_POST) {
            $setsql['name'] = P('name', '广告名称不能为空');
            $setsql['postion'] = P('postion', '', '1');
            $setsql['type'] = P('type', '', '1');
            $start_time = P('start_time', '起始时间不能为空');
            if (!empty($start_time)) {
                $setsql['start_time'] = strtotime($start_time);
            }
            $end_time = P('end_time', '结束时间不能为空');
            if (!empty($end_time)) {
                $setsql['end_time'] = strtotime($end_time);
            }
            $setsql['link'] = P('link');
            if ($_POST['type'] == '1') {
                $setsql['content'] = P('content', '文字不能为空');
            } else {
                if ($_POST['type'] == '2') {
                    $setsql['content'] = P('images', '图片不能为空');
                } else {
                    if ($_POST['type'] == '3') {
                        $setsql['content'] = P('code', '代码不能为空');
                    }
                }
            }
            $setsql['color'] = P('color');
            $setsql['status'] = P('status', '', '1');
            $setsql['pass'] = P('pass', '', '1');
            $setsql['addtime'] = time();
            $id = P('id', 0);
            if ($id) {
                $query = $this->db->update(table('ad'), $setsql, 'id = ' . $id);
            } else {
                $query = $this->db->add(table('ad'), $setsql);
            }
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function del()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : exit('id not null');
        $row = $this->db->get_one("select * from " . table('ad') . " where id = " . $id);
        if ($row['type'] == '2') {
            @unlink($row['content']);
        }
        $query = $this->db->delete(table('ad'), 'id = ' . $id);
        if ($query) {
            showmsg('删除成功');
        } else {
            showmsg('删除失败');
        }
    }
    public function postion()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $all = $this->db->get_all("select * from " . table('ad_postion') . " order by id,px desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($all) {
            foreach ($all as $v) {
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(*) as total from " . table('ad_postion'));
            $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function postion_edit()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table("ad_postion") . " where id = " . $id);
            $this->assign('row', $row);
        }
        $this->display();
    }
    public function postion_save()
    {
        if ($_POST) {
            $alias = P('alias', '调用标签不能为空');
            $setsql['name'] = P('name', '名称不能为空');
            $setsql['alias'] = $alias;
            $setsql['postion'] = P('postion', '', '0');
            $setsql['px'] = P('px', '', '0');
            $setsql['addtime'] = time();
            $id = P('id', 0);
            if ($id) {
                $row = $this->db->get_one("select * from " . table('ad_postion') . " where alias = '" . $alias . "' and id != " . $id);
                if ($row['id']) {
                    error_json('改标签已经纯在');
                } else {
                    $query = $this->db->update(table('ad_postion'), $setsql, 'id = ' . $id);
                }
            } else {
                $row = $this->db->get_one("select * from " . table('ad_postion') . " where alias = '" . $alias . "'");
                if ($row['id']) {
                    error_json('改标签已经纯在');
                } else {
                    $query = $this->db->add(table('ad_postion'), $setsql);
                }
            }
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function postion_del()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : exit('ID不能为空');
        $query = $this->db->delete(table('ad_postion'), 'id =' . $id);
        if ($query) {
            showmsg('删除成功');
        } else {
            showmsg('删除失败');
        }
    }
    private function get_postion()
    {
        return $this->db->get_all("select * from " . table('ad_postion') . " order by id desc");
    }
    private function ad_type()
    {
        return array('1' => '文本', '2' => '图片', '3' => '代码');
    }
    public function link()
    {
        $list = $this->db->get_all("select * from " . table('link') . " order by px,id desc");
        if ($list) {
            foreach ($list as $v) {
                $v['addtime'] = _date($v['addtime']);
                $data[] = $v;
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function link_edit()
    {
        $id = !empty($_GET['id']) ? intval($_GET['id']) : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('link') . " where id = " . $id);
            $this->assign('row', $row);
        }
        $this->display();
    }
    public function link_save()
    {
        if ($_POST) {
            $setsql['name'] = P('name', '名称不能为空');
            $setsql['link'] = P('link', '链接不能为空');
            $setsql['px'] = P('link', '', '0');
            $setsql['status'] = P('status', '', '0');
            $setsql['addtime'] = time();
            $id = P('id', false);
            if ($id) {
                $query = $this->db->update(table('link'), $setsql, 'id = ' . $id);
            } else {
                $query = $this->db->add(table('link'), $setsql);
            }
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function link_del()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : exit('ID IS NOT NULL');
        $query = $this->db->delete(table('link'), 'id = ' . $id);
        if ($query) {
            showmsg('删除成功');
        } else {
            showmsg('删除失败');
        }
    }
}